package top.leyi.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.ICommService;
import top.leyi.entity.Commodity;
import top.leyi.entity.user.User;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;

@Controller
@RequestMapping("/api/trans")
@RestController
public class TransHandle {
    @Autowired
    ICommService commService;

    @GetMapping("/{page}")
    public Result getTrans(@PathVariable("page") int page,
                           @RequestParam("token") String token,
                           @RequestParam(value= "key",required = false, defaultValue = "") String key){

      return new Result(200,commService.getAllCommdities(page, key));
    }

    @PostMapping("")
    public Result addTrans(
                            @RequestParam("token") String token,
                            @RequestParam(value = "price",required = false, defaultValue = "暂未报价") String price,
                            @RequestParam(value = "name" ) String name,
                            @RequestParam(value = "phone" ) String phone,
                            @RequestParam(value = "descripe",required = false,defaultValue = "暂无描述") String descripe,
                            @RequestParam(value = "userId" ) int userId,
                            @RequestParam(value = "pics",required = false) String pics){

        Commodity commodity = new Commodity();
        commodity.setName(name);
        commodity.setPrice(price);
        commodity.setPhone(phone);
        commodity.setDescripe(descripe);
        commodity.setUserId(userId);
        commodity.setPics(pics);

        User user = new User();
        user.setId(userId);
        user.setToken(token);
        commService.addOneCommo(commodity, user);
        return ResultGenerator.OK.getResult();
    }
}
